#include "search_music.h"

#include <QIcon>
#include <QSizePolicy>
#include <QPainter>
#include <QDebug>

search_music::search_music(QWidget *parent)
    : Widget_class(parent)
{
    setupUI();
    setupConnections();

    // 设置固定尺寸
    setFixedSize(240, 190);

    // 设置样式
    setStyleSheet("background-color: white; border-radius: 10px;");
}
search_music::~search_music()
{
}
void search_music::setCatIcon(const QPixmap &pixmap)
{
    if (pixmap.isNull()) return;

    // 简单缩放并保持比例
    QPixmap scaledPixmap = pixmap.scaled(60, 60, Qt::KeepAspectRatio, Qt::SmoothTransformation);

    // 创建一个新的QPixmap，确保背景透明
    QPixmap finalPixmap(scaledPixmap.size());
    finalPixmap.fill(Qt::transparent);

    QPainter painter(&finalPixmap);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setRenderHint(QPainter::SmoothPixmapTransform);
    painter.drawPixmap(0, 0, scaledPixmap);

    catLabel->setPixmap(finalPixmap);
    catLabel->setFixedSize(finalPixmap.size());
}
void search_music::setupUI()
{
    // 创建控件
    catLabel = new QLabel(this);
    catLabel->setAlignment(Qt::AlignCenter);
    catLabel->setStyleSheet("background: transparent; border: none;");

    // 不设置固定尺寸，让标签根据内容自适应
    catLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);

    searchLineEdit = new QLineEdit(this);
    searchLineEdit->setPlaceholderText("输入歌曲名...");
    searchLineEdit->setFixedHeight(30);
    searchLineEdit->setStyleSheet(
        "QLineEdit {"
        "    border: 1px solid #ccc;"
        "    border-radius: 15px;"
        "    padding: 5px 10px;"
        "    font-size: 12px;"
        "}"
    );

    searchButton = new QPushButton(this);
    searchButton->setFixedSize(40, 40);

    // 修改按钮样式：完全透明，只有图标
    searchButton->setStyleSheet(
        "QPushButton {"
        "    background: transparent;"  // 透明背景
        "    border: none;"            // 无边框
        "}"
        "QPushButton:hover {"
        "    background: rgba(0, 0, 0, 0.1);"  // 悬停时轻微背景
        "    border-radius: 20px;"             // 圆形悬停效果
        "}"
        "QPushButton:pressed {"
        "    background: rgba(0, 0, 0, 0.2);"  // 按下时稍深的背景
        "    border-radius: 20px;"
        "}"
    );
    setSearchButtonIcon(QPixmap(":/搜索"));
    // 设置布局
    mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(20, 15, 20, 15);
    mainLayout->setSpacing(20);

    // 添加控件到布局
    mainLayout->addWidget(catLabel);
    mainLayout->addWidget(searchLineEdit);
    mainLayout->addWidget(searchButton, 0, Qt::AlignHCenter);

    // 设置布局中猫猫标签的对齐方式
    mainLayout->setAlignment(catLabel, Qt::AlignHCenter);
    setCatIcon(QPixmap(":/猫"));

    setLayout(mainLayout);
}

void search_music::setupConnections()
{
    connect(searchButton, &QPushButton::clicked, this, &search_music::onSearchClicked);
    connect(searchLineEdit, &QLineEdit::returnPressed, this, &search_music::onSearchClicked);
}


void search_music::setSearchButtonIcon(const QPixmap &pixmap)
{
    QIcon icon(pixmap.scaled(20, 20, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    searchButton->setIcon(icon);
    searchButton->setIconSize(QSize(20, 20));
}

QString search_music::searchText() const
{
    return searchLineEdit->text();
}

void search_music::onSearchClicked()
{
    qDebug()<<"点击了搜索";
    emit searchRequested(searchLineEdit->text());
}
